<template>
  <p-dialog :visible="visible" @close="$emit('close')">
    <h2>绑定设备ID</h2>
    <el-alert style="margin-top:10px;margin-bottom: 10px;" type="success" :closable="false">
      在程序内购买后可以在此通过设备ID获取到订单记录，方便下载离线激活文件。
    </el-alert>

    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="设备ID" prop="deviceId">
        <el-input v-model="form.deviceId" placeholder="请输入设备ID"></el-input>
      </el-form-item>
      <el-form-item>
        <div class="text-right">
          <el-button size="mini" :disabled="loading" type="primary" @click="submitForm()">立即绑定</el-button>
        </div>
      </el-form-item>
    </el-form>
  </p-dialog>
</template>

<script>
import api from "@/utils/api";

export default {
  name: "DeviceBind",
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: false,
      form: {
        deviceId: null
      },
      rules: {
        deviceId: [
          {required: true, message: '请输入设备ID', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 1 到 100 个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    submitForm() {
      let self = this;
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          api.orderDeviceBind(this.form.deviceId).then(res => {
            if (res.code === 1) {
              this.$message.success('绑定成功');
              this.$emit('close');
              this.$emit('refresh');
            }
          }).finally(() => self.loading = false);
        }
      });
    }
  }
}
</script>

<style scoped>

</style>
